package com.example.flight.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flight.common.JwtUtils;
import com.example.flight.common.Result;
import io.jsonwebtoken.Claims;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import com.example.flight.entity.UserEntity;
import com.example.flight.service.UserService;

import javax.servlet.http.HttpServletRequest;



/**
 * 
 *
 * @author hws
 * @email 231884142@qq.com
 * @date 2023-06-07
 */


@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserEntity user){
        //1.数据库根据手机号搜索用户
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getPhone,user.getPhone());
        UserEntity userInDB = userService.getOne(queryWrapper);
        //2.判断用户是否存在
        if( null==userInDB) return Result.error().message("无此用户");
        //3.判断密码是否正确
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!userInDB.getPassword().equals(password)){
            return Result.error().message("密码错误");
        }
        //4.生成token
        String token = JwtUtils.generateToken("普通用户", userInDB.getUid());

        return Result.ok().data("token",token);
    }

    /**
     * 信息
     */
    @GetMapping("/{uid}")
    public Result info(@PathVariable("uid") String uid){
		UserEntity user = userService.getById(uid);
        return Result.ok().data("user", user);
    }
    /**
     * 登录状态
     */
    @GetMapping()
    public Result loginInfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring("Bearer ".length());
        }
        Claims claims = JwtUtils.getClaimsByToken(token);
        UserEntity user = userService.getById((String) claims.get("id"));
        return Result.ok().data("user", user);
    }


    /**
     * 注册
     */
    @PostMapping("/register")
    public Result save(@RequestBody UserEntity user){
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getPhone,user.getPhone());
        UserEntity userInDB = userService.getOne(queryWrapper);
        if( null==userInDB){
            String password = user.getPassword();
            user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
            return Result.ok().success(userService.save(user));
        }

        return Result.error().message("手机号已被注册");
    }

    /**
     * 修改
     */
    @PutMapping
    public Result update(@RequestBody UserEntity user){
		userService.updateById(user);

        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{uid}")
    public Result delete(@PathVariable("uid") String uid){
		userService.removeById(uid);

        return Result.ok();
    }

}
